<template>
  <div class="app-container">
      <!--搜索区-->
      <el-card class="filter-container" shadow="never">
          <div>
              <i class="el-icon-search"></i>
              <span>筛选搜索</span>
              <el-button style="float:right" type="primary" @click="handleQuery()" size="small">
                  查询搜索
              </el-button>
              <el-button style="float:right;margin-right: 15px" @click="resetQuery()" size="small">
                  重置
              </el-button>
              <div style="margin-top: 15px">
                  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">
                    <el-form-item label="房源类型" prop="room">
                      <el-select v-model="queryParams.type" placeholder="请选择" clearable >
                        <el-option
                          v-for="dict in dict.type.house_type"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-form>
              </div>
          </div>
      </el-card>

      <!--操作按钮区-->
      <el-card class="operate-container" shadow="never">
          <i class="el-icon-tickets"></i>
          <span>数据列表</span>
          <el-button style="margin-left: 20px" type="primary" plain icon="el-icon-circle-plus" size="mini" @click="handleAdd" v-hasPermi="['basic:house:create']">新增</el-button>
      </el-card>

      <!--数据列表区-->
      <div class="table-container">
          <el-table v-loading="loading" :data="houseList" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column prop="orderNo" label="房源编号" width="100"/>
            <el-table-column prop="title" label="房源标题" />
            <el-table-column label="房源类型" align="center" prop="type" width="80">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.house_type" :value="scope.row.type"/>
              </template>
            </el-table-column>
            <el-table-column prop="area" label="房屋面积" width="100"/>
            <el-table-column prop="villageName" label="小区/公寓名称" width="150"/>
            <el-table-column label="房源状态" align="center" prop="state" width="80">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.house_states" :value="scope.row.state"/>
              </template>
            </el-table-column>
            <el-table-column prop="money" label="租金" width="80"/>
            <el-table-column label="状态" align="center" prop="isshow" width="50">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_show_list" :value="scope.row.isshow"/>
              </template>
            </el-table-column>
            <el-table-column prop="realName" label="发布用户名" width="50"/>
            <el-table-column prop="phone" label="用户手机" width="50"/>
            <el-table-column prop="adminId" label="管理员" width="50"/>
            <el-table-column prop="commission" label="佣金比例" width="50"/>
            <el-table-column  label="发布时间" width="150">
              <template slot-scope="scope">
                <span>{{ dateTimeFormat(scope.row.pushDate) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['basic:house:edit']"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['basic:house:remove']"
                    >删除</el-button>
                </template>
            </el-table-column>
          </el-table>
      </div>

      <!--分页区-->
      <div class="pagination-container">
          <el-pagination
                  layout="total, sizes,prev, pager, next,jumper"
                  :current-page="queryParams.pageNum"
                  :page-size="queryParams.pageSize"
                  :page-sizes="[10,15,20]"
                  :total="total"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
          />
      </div>

    <!-- 添加或修改房源表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHouse, getHouse, delHouse, addHouse, updateHouse } from "@/api/basic/house";

export default {
  name: "House",
  dicts: ['house_type','sys_show_list','house_states'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房源表表格数据
      houseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询房源表列表 */
    getList() {
      this.loading = true;
      listHouse(this.queryParams).then(response => {
        this.houseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        houseId: null,
        orderNo: null,
        title: null,
        type: null,
        hall: null,
        room: null,
        toilet: null,
        balcony: null,
        kitchen: null,
        area: null,
        orienta: null,
        villageId: null,
        name: null,
        address: null,
        zoneId: null,
        subway: null,
        subwayName: null,
        lng: null,
        lat: null,
        mapAddress: null,
        config: null,
        tags: null,
        decoration: null,
        state: null,
        rzDate: null,
        viewTime: null,
        ramark: null,
        money: null,
        payType: null,
        requirement: null,
        isshow: null,
        userid: null,
        date: null,
        adminId: null,
        commission: null,
        istop: null,
        startMonth: null,
        endMonth: null,
        shortRent: null,
        deposit: null,
        serviceMoney: null,
        isagency: null,
        agencyMoney: null,
        updatedTime: null,
        updatedBy: null,
        createdTime: null,
        createdBy: null,
        deleted: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.houseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path:'/basic/house/houseAdd'});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path:'/basic/house/houseAdd',query:{id:row.houseId}});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.houseId != null) {
            updateHouse(this.form.houseId,this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const houseIds = row.houseId || this.ids;
      this.$modal.confirm('是否确认删除房源表编号为"' + houseIds + '"的数据项？').then(function() {
        return delHouse(houseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleSizeChange(val) {
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = val;
        this.getList();
    },
    handleCurrentChange(val) {
        this.queryParams.pageNum = val;
        this.getList();
    },
  }
};
</script>
